package hierarchy

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/hierarchy"
)

type FdArticleService struct {
}

func (fdArticleService *FdArticleService) GetArticle(level string, levelID string, userID uint) (fdArticle []hierarchy.FdArticle, err error) {
	//var fdVideoArticle []hierarchy.FdArticle
	//var fdGraphicArticle []hierarchy.FdArticle
	//var fdAudioArticle []hierarchy.FdArticle
	var fdVideoArticle []hierarchy.FdArticle
	var fdGraphicArticle []hierarchy.FdArticle
	var fdAudioArticle []hierarchy.FdArticle
	where := ""

	switch level {
	case "one":
		where = "level_one_id = ? AND status = 1"
	case "two":
		where = "level_two_id = ? AND status = 1"
	case "three":
		where = "level_three_id = ? AND status = 1"

	}
	err = global.GVA_DB.Model(&hierarchy.FdArticleVideo{}).
		Preload("FdArticleLike", "user_id = ? and fd_article_type = ?", userID, "video").
		Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "video").
		Where(where, levelID).Find(&fdVideoArticle).Error
	if err != nil {
		return nil, err
	}
	err = global.GVA_DB.Model(&hierarchy.FdArticleGraphic{}).
		Preload("FdArticleLike", "user_id = ? and fd_article_type = ?", userID, "graphic").
		Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "graphic").
		Where(where, levelID).Find(&fdGraphicArticle).Error
	if err != nil {
		return nil, err
	}
	err = global.GVA_DB.Model(&hierarchy.FdArticleAudio{}).
		Preload("FdArticleLike", "user_id = ? and fd_article_type = ?", userID, "audio").
		Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "audio").
		Where(where, levelID).Find(&fdAudioArticle).Error
	if err != nil {
		return nil, err
	}
	for i := range fdVideoArticle {
		fdVideoArticle[i].Type = hierarchy.VIDEO
	}
	for i := range fdGraphicArticle {
		fdGraphicArticle[i].Type = hierarchy.GRAPHIC
	}
	for i := range fdAudioArticle {
		fdAudioArticle[i].Type = hierarchy.AUDIO
	}
	fdArticle = append(fdArticle, fdVideoArticle...)
	fdArticle = append(fdArticle, fdGraphicArticle...)
	fdArticle = append(fdArticle, fdAudioArticle...)
	for fdArticleIndex := range fdArticle {
		//if fdArticle[fdArticleIndex].PV != "" {
		//	num, _ := strconv.ParseFloat(fdArticle[fdArticleIndex].PV, 64)
		//	if num < 200 {
		//		// 浏览量直接拼接“w+”
		//		fdArticle[fdArticleIndex].PV = fmt.Sprintf("%dw+", int(num))
		//	} else {
		//		fdArticle[fdArticleIndex].PV = fmt.Sprintf("%dw+", int(num))
		//	}
		//} else {
		//	fdArticle[fdArticleIndex].PV = "1w+"
		//}
		if fdArticle[fdArticleIndex].FdArticleCollect == nil {
			fdArticle[fdArticleIndex].IsCollect = false
		} else {
			fdArticle[fdArticleIndex].IsCollect = true
		}
		if fdArticle[fdArticleIndex].FdArticleLike == nil {
			fdArticle[fdArticleIndex].IsPraise = false
		} else {
			fdArticle[fdArticleIndex].IsPraise = true
		}
	}
	SortArr(fdArticle)
	return fdArticle, err
}

// GetHotArticle 搜索文章&获取热门搜索文章
func (fdArticleService *FdArticleService) GetHotArticle(userID uint, title string) (fdArticle []hierarchy.HotSearch, err error) {
	var hotSearch []hierarchy.HotSearch
	var fdVideoArticle []hierarchy.FdArticleVideo
	var fdGraphicArticle []hierarchy.FdArticleGraphic
	var fdAudioArticle []hierarchy.FdArticleAudio
	var fdArticleCollect []hierarchy.FdArticleCollect
	var videoIDs []uint
	var audioIDs []uint
	var graphicIDs []uint
	global.GVA_DB.Find(&fdArticleCollect, "user_id = ?", userID)

	for i := range fdArticleCollect {
		if fdArticleCollect[i].FdArticleType == "video" {
			videoIDs = append(videoIDs, fdArticleCollect[i].FdArticleID)
		}
		if fdArticleCollect[i].FdArticleType == "audio" {
			audioIDs = append(audioIDs, fdArticleCollect[i].FdArticleID)
		}
		if fdArticleCollect[i].FdArticleType == "graphic" {
			graphicIDs = append(graphicIDs, fdArticleCollect[i].FdArticleID)
		}
	}

	if title == "" {
		verr := global.GVA_DB.
			Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "video").
			Preload("LevelTwo").Find(&fdVideoArticle).Order("search_number desc").Limit(10).Error
		if verr != nil {
			return nil, verr
		}
		gerr := global.GVA_DB.
			Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "graphic").
			Preload("LevelTwo").Find(&fdGraphicArticle).Order("search_number desc").Limit(10).Error
		if gerr != nil {
			return nil, gerr
		}
		aerr := global.GVA_DB.
			Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "audio").
			Preload("LevelTwo").Find(&fdAudioArticle).Order("search_number desc").Limit(10).Error
		if aerr != nil {
			return nil, aerr
		}
	} else {
		verr := global.GVA_DB.
			Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "video").
			Preload("LevelTwo").Where("title like ?", "%"+title+"%").Order("search_number desc").Limit(10).Find(&fdVideoArticle).Error
		if verr != nil {
			return nil, verr
		}
		gerr := global.GVA_DB.
			Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "graphic").
			Preload("LevelTwo").Where("title like ?", "%"+title+"%").Order("search_number desc").Limit(10).Find(&fdGraphicArticle).Error
		if gerr != nil {
			return nil, gerr
		}
		aerr := global.GVA_DB.
			Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "audio").
			Preload("LevelTwo").Where("title like ?", "%"+title+"%").Order("search_number desc").Limit(10).Find(&fdAudioArticle).Error
		if aerr != nil {
			return nil, aerr
		}
	}

	for i := range fdVideoArticle {
		if fdVideoArticle[i].FdArticleCollect != nil {
			fdVideoArticle[i].IsCollect = true
		} else {
			fdVideoArticle[i].IsCollect = false
		}
		hotSearch = append(hotSearch, hierarchy.HotSearch{
			ID:           fdVideoArticle[i].ID,
			LevelTwoID:   *fdVideoArticle[i].LevelTwoID,
			Briefly:      fdVideoArticle[i].Briefly,
			Key:          fdVideoArticle[i].LevelTwo.Key,
			SearchNumber: fdVideoArticle[i].SearchNumber,
			IsVip:        fdVideoArticle[i].IsVip,
			PraiseNum:    fdVideoArticle[i].PraiseNum,
			ForwardNum:   fdVideoArticle[i].ForwardNum,
			IsCollect:    fdVideoArticle[i].IsCollect,
			Title:        fdVideoArticle[i].Title,
			Cover:        fdVideoArticle[i].Cover,
			Types:        "video",
		})
	}
	for i := range fdGraphicArticle {
		if fdGraphicArticle[i].FdArticleCollect != nil {
			fdGraphicArticle[i].IsCollect = true
		} else {
			fdGraphicArticle[i].IsCollect = false
		}
		hotSearch = append(hotSearch, hierarchy.HotSearch{
			ID:           fdGraphicArticle[i].ID,
			LevelTwoID:   *fdGraphicArticle[i].LevelTwoID,
			Key:          fdGraphicArticle[i].LevelTwo.Key,
			Briefly:      fdGraphicArticle[i].Briefly,
			SearchNumber: fdGraphicArticle[i].SearchNumber,
			IsVip:        fdGraphicArticle[i].IsVip,
			PraiseNum:    fdGraphicArticle[i].PraiseNum,
			ForwardNum:   fdGraphicArticle[i].ForwardNum,
			IsCollect:    fdGraphicArticle[i].IsCollect,
			Title:        fdGraphicArticle[i].Title,
			Cover:        fdGraphicArticle[i].Cover,
			Types:        "graphic",
		})
	}
	for i := range fdAudioArticle {
		if fdAudioArticle[i].FdArticleCollect != nil {
			fdAudioArticle[i].IsCollect = true
		} else {
			fdAudioArticle[i].IsCollect = false
		}
		hotSearch = append(hotSearch, hierarchy.HotSearch{
			ID:           fdAudioArticle[i].ID,
			LevelTwoID:   *fdAudioArticle[i].LevelTwoID,
			Key:          fdAudioArticle[i].LevelTwo.Key,
			SearchNumber: fdAudioArticle[i].SearchNumber,
			IsVip:        fdAudioArticle[i].IsVip,
			PraiseNum:    fdAudioArticle[i].PraiseNum,
			ForwardNum:   fdAudioArticle[i].ForwardNum,
			IsCollect:    fdAudioArticle[i].IsCollect,
			Briefly:      fdAudioArticle[i].Briefly,
			Title:        fdAudioArticle[i].Title,
			Cover:        fdAudioArticle[i].Cover,
			Types:        "audio",
		})
	}

	SortHotSearch(hotSearch)
	if len(hotSearch) >= 10 {
		hotSearch = hotSearch[:10]
	}

	if title != "" {
		for i := range hotSearch {
			table := ""
			switch hotSearch[i].Types {
			case "video":
				table = "fd_article_video"
			case "graphic":
				table = "fd_article_graphic"
			case "audio":
				table = "fd_article_audio"
			}
			err = global.GVA_DB.Table(table).Where("id = ?", hotSearch[i].ID).Update("search_number", hotSearch[i].SearchNumber+1).Error
		}
	}

	return hotSearch, err
}

// ForwardArticle 转发文章
func (fdArticleService *FdArticleService) ForwardArticle(types, id string) (err error) {
	switch types {
	case "video":
		var videoArticle hierarchy.FdArticleVideo
		if err = global.GVA_DB.Where("id = ?", id).First(&videoArticle).Error; err != nil {
			return err
		}
		err = global.GVA_DB.Model(&hierarchy.FdArticleVideo{}).Where("id = ?", id).Update("forward_num", videoArticle.ForwardNum+1).Error
	case "graphic":
		var graphicArticle hierarchy.FdArticleGraphic
		if err = global.GVA_DB.Where("id = ?", id).First(&graphicArticle).Error; err != nil {
			return err
		}
		err = global.GVA_DB.Model(&hierarchy.FdArticleGraphic{}).Where("id = ?", id).Update("forward_num", graphicArticle.ForwardNum+1).Error
	case "audio":
		var audioArticle hierarchy.FdArticleAudio
		if err = global.GVA_DB.Where("id = ?", id).First(&audioArticle).Error; err != nil {
			return err
		}
		err = global.GVA_DB.Model(&hierarchy.FdArticleAudio{}).Where("id = ?", id).Update("forward_num", audioArticle.ForwardNum+1).Error
	}
	return
}

// 根据数组中的SearchNumber字段[int]倒叙排序
func SortHotSearch(articles []hierarchy.HotSearch) {
	for i := 0; i < len(articles)-1; i++ {
		for j := 0; j < len(articles)-1-i; j++ {
			if articles[j].SearchNumber < articles[j+1].SearchNumber {
				articles[j], articles[j+1] = articles[j+1], articles[j]
			}
		}
	}
}

// 根据数组中的created字段[time.Time]倒叙排序
func SortArr(articles []hierarchy.FdArticle) {
	for i := 0; i < len(articles)-1; i++ {
		for j := 0; j < len(articles)-1-i; j++ {
			if articles[j].CreatedAt.Before(articles[j+1].CreatedAt) {
				articles[j], articles[j+1] = articles[j+1], articles[j]
			}
		}
	}
}

// 根据数组中的created字段[time.Time]倒叙排序
func NewSortArr(articles []hierarchy.ResArticle) {
	for i := 0; i < len(articles)-1; i++ {
		for j := 0; j < len(articles)-1-i; j++ {
			if articles[j].CreatedAt.Before(articles[j+1].CreatedAt) {
				articles[j], articles[j+1] = articles[j+1], articles[j]
			}
		}
	}
}

func (fdArticleService *FdArticleService) GetMyCollect(userID uint) (fdArticle []hierarchy.FdArticle, err error) {
	var fdVideoArticle []hierarchy.FdArticle
	var fdGraphicArticle []hierarchy.FdArticle
	var fdAudioArticle []hierarchy.FdArticle
	var fdArticleCollect []hierarchy.FdArticleCollect
	var videoIDs []uint
	var graphicIDs []uint
	var audioIDs []uint

	global.GVA_DB.Find(&fdArticleCollect, "user_id = ?", userID)

	for i := range fdArticleCollect {
		if fdArticleCollect[i].FdArticleType == "video" {
			videoIDs = append(videoIDs, fdArticleCollect[i].FdArticleID)
		}
		if fdArticleCollect[i].FdArticleType == "audio" {
			audioIDs = append(audioIDs, fdArticleCollect[i].FdArticleID)
		}
		if fdArticleCollect[i].FdArticleType == "graphic" {
			graphicIDs = append(graphicIDs, fdArticleCollect[i].FdArticleID)
		}
	}

	err = global.GVA_DB.Model(&hierarchy.FdArticleVideo{}).
		Preload("FdArticleLike", "user_id = ? and fd_article_type = ?", userID, "video").
		Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "video").Find(&fdVideoArticle, "id in (?)", videoIDs).Error
	if err != nil {
		return nil, err
	}
	err = global.GVA_DB.Model(&hierarchy.FdArticleGraphic{}).
		Preload("FdArticleLike", "user_id = ? and fd_article_type = ?", userID, "graphic").
		Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "graphic").Find(&fdGraphicArticle, "id in (?)", graphicIDs).Error
	if err != nil {
		return nil, err
	}
	err = global.GVA_DB.Model(&hierarchy.FdArticleAudio{}).
		Preload("FdArticleLike", "user_id = ? and fd_article_type = ?", userID, "audio").
		Preload("FdArticleCollect", "user_id = ? and fd_article_type = ?", userID, "audio").Find(&fdAudioArticle, "id in (?)", audioIDs).Error
	if err != nil {
		return nil, err
	}
	for i := range fdVideoArticle {
		fdVideoArticle[i].Type = hierarchy.VIDEO
	}
	for i := range fdGraphicArticle {
		fdGraphicArticle[i].Type = hierarchy.GRAPHIC
	}
	for i := range fdAudioArticle {
		fdAudioArticle[i].Type = hierarchy.AUDIO
	}

	fdArticle = append(fdArticle, fdVideoArticle...)
	fdArticle = append(fdArticle, fdGraphicArticle...)
	fdArticle = append(fdArticle, fdAudioArticle...)

	for fdArticleIndex := range fdArticle {
		if fdArticle[fdArticleIndex].FdArticleCollect == nil {
			fdArticle[fdArticleIndex].IsCollect = false
		} else {
			fdArticle[fdArticleIndex].IsCollect = true
		}
		if fdArticle[fdArticleIndex].FdArticleLike == nil {
			fdArticle[fdArticleIndex].IsPraise = false
		} else {
			fdArticle[fdArticleIndex].IsPraise = true
		}
	}

	SortArr(fdArticle)
	return fdArticle, err
}
